﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace syc.Store
{
    public partial class 原料入库单管理 : System.Web.UI.Page
    {

        private string ss =
@"select
原料入库单.ID,
原料入库单.单据号,
原料.品名+'('+原料.钢级+')' as 原料,
原料.规格,
原料供货单位.名称 as 供货单位,
原料入库单.数量,
原料入库单.重量,
原料入库单.日期,
原料入库单.库管员 from 原料入库单
inner join 原料 on 原料入库单.原料ID=原料.ID
inner join 原料供货单位 on 原料入库单.供货单位ID=原料供货单位.ID";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string opp3 = (string)Session["OPP3"];
#if DEBUG
                opp3 = ",1,2,3,4,5,6,";
#endif
                if (opp3 == null || opp3 == ",")
                {
                    this.Panel1.Visible = false;
                    this.Panel2.Visible = false;
                    this.Panel3.Visible = false;
                    this.Message.Text = "·权限不足";
                    return;
                }
                else
                {
                    if (opp3.StartsWith(","))
                    {
                        opp3 = opp3.Substring(1);
                    }
                    if (opp3.EndsWith(","))
                    {
                        opp3 = opp3.Substring(0, opp3.Length - 1);
                    }
                    this.SqlDataSource2.SelectCommand = "select [ID],[品名] from [原料] where [ID] in (" + opp3 + ")";
                    this.YuanLiao.DataBind();
                    this.GongHuo.DataBind();
                    this.Button_Query_Click(null, null);
                }
            }
            else
            {
                this.SqlDataSource1.SelectCommand = this.SqlText.Text;
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SelectedID.Text = this.GridView1.SelectedDataKey[0].ToString();
            this.Button_Delete.Enabled = true;
            this.Button_View.Enabled = true;
            this.Button_Related.Enabled = true;
        }
        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.DeleteCommand = "YuanLiao_In_Bill_Delete "
                + this.SelectedID.Text;
            this.SqlDataSource1.Delete();
            this.Message.Text = "·删除命令完毕，如果入库单没有被删除则可能是因为其中原料已经出库";
            this.Button_View.Enabled = false;
            this.Button_Related.Enabled = false;
            this.Button_Delete.Enabled = false;
            this.GridView1.SelectedIndex = -1;

        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string sql = " where 原料入库单.原料ID=" + this.YuanLiao.Text;


            if (this.GongHuo.Text != "0")
            {
                sql += " and 原料入库单.供货单位ID=" + this.GongHuo.Text;
            }

            DateTime d1, d2;
            int n = 0;
            int.TryParse(this.DanHao.Text, out n);
            if (n>0)
            {
                sql += " and 原料入库单.单据号="+n;
            }
            else
            {
                if (this.Date1.Text != "")
                {
                    if (DateTime.TryParse(this.Date1.Text, out d1))
                    {
                        sql += " and 原料入库单.日期>='" + d1.ToShortDateString() + "'";
                    }
                }

                if (this.Date2.Text != "")
                {
                    if (DateTime.TryParse(this.Date2.Text, out d2))
                    {
                        sql += " and 原料入库单.日期<='" + d2.ToShortDateString() + "'";
                    }
                }

            }
            sql += "order by 原料入库单.ID desc";
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = ss + sql;
            this.SqlText.Text = ss+sql;
            this.GridView1.SelectedIndex = -1;
            this.Button_View.Enabled = false;
            this.Button_Related.Enabled = false;
            this.Button_Delete.Enabled = false;
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            this.GongHuo.Text = "0";
            this.DanHao.Text = "";
            this.Date1.Text = "";
            this.Date2.Text = "";
        }

    }
}
